﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.MediaCenter.UI;

namespace RadioStream.Code.Utils
{
    class XMLUtil
    {
        static public string GetFirstElementText(XmlDocument doc, string XPath, XmlNamespaceManager XNsM)
        {
            string retString = "";
            List<string> retList = GetElementTexts(doc, XPath, XNsM);
            if (retList.Count > 0)
            {
                retString = retList[0];
            }
            return retString;
        }

        static public List<string> GetElementTexts(XmlDocument doc, string XPath, XmlNamespaceManager XNsM)
        {
            List<string> retStrList = new List<string>();
            XmlNode root = doc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes(XPath, XNsM);
            foreach (XmlNode Node in nodes)
            {
                retStrList.Add(Node.InnerText);
            }
            return retStrList;
        }

        static public List<XmlAttributeCollection> GetElementAttributes(XmlDocument doc, string XPath, XmlNamespaceManager XNsM)
        {
            List<XmlAttributeCollection> retAttrList = new List<XmlAttributeCollection>();
            XmlNode root = doc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes(XPath, XNsM);
            foreach (XmlNode Node in nodes)
            {
                retAttrList.Add(Node.Attributes);
            }
            return retAttrList;
        }


        public static XmlDocument GetXMLDocument(string stDocURI)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(stDocURI);
            return doc;
        }

        //static public XmlDocument FromHtml(TextReader reader)
        //{

        //    // setup SgmlReader
        //    Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
        //    sgmlReader.DocType = "HTML";
        //    sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
        //    sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
        //    sgmlReader.InputStream = reader;

        //    // create document
        //    XmlDocument doc = new XmlDocument();
        //    doc.PreserveWhitespace = true;
        //    doc.XmlResolver = null;
        //    doc.Load(sgmlReader);
        //    return doc;
        //}
        static public void WriteXML(string FileURI, ArrayListDataSet StationList, ArrayListDataSet RegionList)
        {
            XmlTextWriter myXmlTextWriter = new XmlTextWriter(FileURI,
                                                             System.Text.Encoding.UTF8);
            myXmlTextWriter.Formatting = Formatting.Indented;

            myXmlTextWriter.WriteStartDocument();

            myXmlTextWriter.WriteComment("Written by Radiostream for Media Center");

            myXmlTextWriter.WriteStartElement("RadioStream");
            // add the namespace stuff
            myXmlTextWriter.WriteAttributeString("xmlns", "RadioStream");
            myXmlTextWriter.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            myXmlTextWriter.WriteAttributeString("xsi:schemaLocation", "RadioStream RadioStream.xsd");

            // add the version tag
            myXmlTextWriter.WriteStartElement("Version");
            myXmlTextWriter.WriteAttributeString("Major", "1");
            myXmlTextWriter.WriteAttributeString("Minor", "0");
            myXmlTextWriter.WriteAttributeString("Patch", "0");
            myXmlTextWriter.WriteEndElement();

            //add the stations as a list of single station(s)
            myXmlTextWriter.WriteStartElement("Stations");
            foreach (CStationInfo Station in StationList)
            {
                myXmlTextWriter.WriteStartElement("Station");
                myXmlTextWriter.WriteAttributeString("ID", Station.StationID.ToString());
                myXmlTextWriter.WriteAttributeString("Favorite", Station.IsFavorite.ToString());
                myXmlTextWriter.WriteAttributeString("Region", "0");
                myXmlTextWriter.WriteEndElement();
            }
            myXmlTextWriter.WriteEndElement();

            //add a list of regions
            myXmlTextWriter.WriteStartElement("Regions");
            foreach (CRegionInfo Region in RegionList)
            {
                myXmlTextWriter.WriteStartElement("Region");
                myXmlTextWriter.WriteAttributeString("ID", Region.ID.ToString());
                myXmlTextWriter.WriteAttributeString("Name", Region.Name.ToString());
                myXmlTextWriter.WriteAttributeString("State", Region.State.ToString());
                myXmlTextWriter.WriteEndElement();
            }
            myXmlTextWriter.WriteEndElement();

            myXmlTextWriter.WriteEndElement();


            myXmlTextWriter.Flush();
            myXmlTextWriter.Close();
        }
    }
}
